package com.woquyin.common;

import java.util.regex.Pattern;

/**
 * 常量类
 */
public class Constance {

	public enum WholeStateCodeEnum {
		SUCCESS(200, "成功"), OTHER_ERROR(500, "其他错误"), NOT_EMPTY(501, "不能为空");

		WholeStateCodeEnum(int code, String value) {
			this.code = code;
			this.value = value;
		}

		private int code;
		private String value;

		public int getCode() {
			return code;
		}

		public void setCode(int code) {
			this.code = code;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public static WholeStateCodeEnum codeOf(int code) {
			for (WholeStateCodeEnum wholeStateCodeEnum : values()) {
				if (wholeStateCodeEnum.getCode() == code) {
					return wholeStateCodeEnum;
				}
			}
			throw new RuntimeException("没有找到对应的枚举");
		}

		public static WholeStateCodeEnum statusValueOf(String value) {
			for (WholeStateCodeEnum wholeStateCodeEnum : values()) {
				if (wholeStateCodeEnum.getValue().equals(value)) {
					return wholeStateCodeEnum;
				}
			}
			throw new RuntimeException("没有找到对应的枚举");
		}
	}

	public enum UserStateCodeEnum {
		LOGIN_ERROR(1001, "未登录或登录状态已过期，请登录后再试"), 
		BE_FROZEN(1002, "用户账户被冻结，请联系客服"), 
		VERIFICATION_CODE_ERROR(1003, "验证码不正确"),
		PHONE_FORMAT_NOT_RIGHT(1004, "手机号码格式不正确"),
		EMAIL_FORMAT_NOT_RIGHT(1005, "邮箱格式不正确"),
		UPDATE_USERINFO_ERROR(1006, "修改个人信息失败，请稍后再试"), 
		PHONE_IS_SIGNED(1007, "手机号码已注册"),
		SIGN_ERROR(1008, "注册失败，请稍后重试");

		UserStateCodeEnum(int code, String value) {
			this.code = code;
			this.value = value;
		}

		private int code;
		private String value;

		public int getCode() {
			return code;
		}

		public void setCode(int code) {
			this.code = code;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public static UserStateCodeEnum codeOf(int code) {
			for (UserStateCodeEnum userStateCodeEnum : values()) {
				if (userStateCodeEnum.getCode() == code) {
					return userStateCodeEnum;
				}
			}
			throw new RuntimeException("没有找到对应的枚举");
		}

		public static UserStateCodeEnum statusValueOf(String value) {
			for (UserStateCodeEnum userStateCodeEnum : values()) {
				if (userStateCodeEnum.getValue().equals(value)) {
					return userStateCodeEnum;
				}
			}
			throw new RuntimeException("没有找到对应的枚举");
		}
		
		public static void checkPhone(String phone){
			if(!(phone.length() == 11 && Pattern.compile("^1[34578]\\d{9}$").matcher(phone).find())){
				throw new GrobalRunTimeException(PHONE_FORMAT_NOT_RIGHT.getCode(), PHONE_FORMAT_NOT_RIGHT.getValue());
			}
		}
		
		public static void checkEmail(String email){
			if(!email.equals("") && !Pattern.compile("^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$").matcher(email).find()){
				throw new GrobalRunTimeException(EMAIL_FORMAT_NOT_RIGHT.getCode(), EMAIL_FORMAT_NOT_RIGHT.getValue());
			}
		}
	}

	public enum AddressStateCodeEnum {
		NOT_EMPTY(1011, "不能为空"), 
		PHONE_FORMAT_NOT_RIGHT(1012, "联系方式格式不正确"), 
		ADDRESS_MORE_THAN_20(1013, "最多只能同时存在20个收货地址"), 
		SAVE_ERROR(1014, "保存失败，请稍后再试"), 
		GET_ADDRESS_INFO_ERROR(1015, "获取信息失败，请稍后再试"), 
		DO_NOT_DELETE_DEFAULT_ADDRESS(1016, "不能删除默认地址"), 
		DELETE_ERROR(1017, "删除失败，请稍后再试"), 
		UPDATE_DEFAULT_ADDRESS_ERROR(1018, "更改默认地址失败，请稍后再试");

		AddressStateCodeEnum(int code, String value) {
			this.code = code;
			this.value = value;
		}

		private int code;
		private String value;

		public int getCode() {
			return code;
		}

		public void setCode(int code) {
			this.code = code;
		}

		public String getValue() {
			return value;
		}
		
		public String getValueAddString(String string){
			return string+value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public static AddressStateCodeEnum codeOf(int code) {
			for (AddressStateCodeEnum addressStateCodeEnum : values()) {
				if (addressStateCodeEnum.getCode() == code) {
					return addressStateCodeEnum;
				}
			}
			throw new RuntimeException("没有找到对应的枚举");
		}

		public static AddressStateCodeEnum statusValueOf(String value) {
			for (AddressStateCodeEnum addressStateCodeEnum : values()) {
				if (addressStateCodeEnum.getValue().equals(value)) {
					return addressStateCodeEnum;
				}
			}
			throw new RuntimeException("没有找到对应的枚举");
		}
		
		public static void checkEmpty(String variable, String chineseDescribe){
			if(variable.equals("")){
				throw new GrobalRunTimeException(NOT_EMPTY.getCode(), NOT_EMPTY.getValueAddString(chineseDescribe));
			}
		}

		public static void checkPhone(String phone){
			if(!((phone.length() == 11 && Pattern.compile("^1[34578]\\d{9}$").matcher(phone).find()) || !Pattern.compile("^(0[0-9]{2,3}/-)?([2-9][0-9]{6,7})+(/-[0-9]{1,4})?$").matcher(phone).find())){
				throw new GrobalRunTimeException(PHONE_FORMAT_NOT_RIGHT.getCode(), PHONE_FORMAT_NOT_RIGHT.getValue());
			}
		}
	}
	
	public interface UserStatus {
		String BE_FROZEN = "0";
		String NORMAL = "1";
	}
	
	public interface AddressMaster {
		String DEFAULT = "1";
		String NOT_DEFAULT = "0";
	}
}
